package com.friddle.instance.aliyun;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.tuple.MutablePair;

import com.aliyun.api.AliyunClient;
import com.aliyun.api.DefaultAliyunClient;
import com.aliyun.api.rds.rds20140815.request.CreateDatabaseRequest;
import com.aliyun.api.rds.rds20140815.request.DescribeDBInstancesRequest;
import com.aliyun.api.rds.rds20140815.response.CreateDatabaseResponse;
import com.aliyun.api.rds.rds20140815.response.DescribeDBInstancesResponse;
import com.friddle.instance.DBIntances;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.taobao.api.ApiException;

/**
 * Created by friddle on 15/3/17.
 */
public class AliDBInstance implements DBIntances {
	AliyunClient mClient;
	DBConfigure configure;

	public class DBConfigure {
		public static final String aliUrl = "http://rds.aliyuncs.com";
		public String regionId = Constants.Region_QINGDAO;
		public String characterset = "utf8";
	}

	@Override
	public MutablePair<Boolean, String> initEnvironment(String AccessKey, String AccessSecret, Map<String, String> envs) {
		mClient = new DefaultAliyunClient(DBConfigure.aliUrl, AccessKey, AccessSecret);
		configure = new DBConfigure();
		if (envs != null) {
			if (envs.get("regionId") != null) {
				configure.regionId = envs.get("regionId");
			}
		}
		return null;
	}

	@Override
	public MutablePair<Boolean, String> createDatabase(String databasename, String rdsid, String ownerid, Map<String, String> envs) {
		MutablePair<Boolean, String> result = new MutablePair<>();
		try {
			CreateDatabaseRequest request = new CreateDatabaseRequest();
			request.setdBInstanceId(rdsid);
			request.setdBName(databasename);
			request.setCharacterSetName(configure.characterset);
			request.setOwnerId(ownerid);
			CreateDatabaseResponse response = mClient.execute(request);
			if (response.isSuccess()) {
				result.setLeft(true);
				result.setRight(response.getBody());
			} else {
				result.setLeft(false);
				result.setRight(response.getMessage());
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return result;
	}

	public Map<String, Map<String, String>> getRdsDescript(String region) {
		try {
			DescribeDBInstancesRequest request = new DescribeDBInstancesRequest();
			request.setRegionId(region);
			DescribeDBInstancesResponse response = mClient.execute(request);
			JsonParser parser = new JsonParser();
			JsonObject jsonresponse = parser.parse(response.getBody()).getAsJsonObject();
			JsonArray array = jsonresponse.get("Items").getAsJsonObject().get("DBInstance").getAsJsonArray();
			Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
			for (int i = 0; i < array.size(); i++) {
				String id = array.get(i).getAsJsonObject().get("DBInstanceId").getAsString();
				map.put(id, null);
			}
			return map;
		} catch (ApiException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public MutablePair<Boolean, String> createRdsInstance(String type, Map<String, String> envs) {

		return null;
	}

	@Override
	public MutablePair<Boolean, String> deleteInstances(String instanceid) {
		return null;
	}
}
